package org.tity.test.network;

import java.io.File;
import java.net.URL;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.tity.commons.lang.StringUtil;
import org.tity.commons.network.Ajax;
 

public class WeatherClient {

	private static String url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
//	private static String url = "http://127.0.0.1:8001/soap/sendMessage?wsdl";
// 端点引用 指接口位置 
	private static EndpointReference targetEpr = new EndpointReference(url);
// 有抽象OM工厂获取OM工厂，创建request SOAP包 
	private static OMFactory fac = OMAbstractFactory.getOMFactory();

	public static OMElement getOMMethod(String methodStr, String namespace, String tns, String[] pars, String[] vals) {
		// 创建命名空间 
		OMNamespace nms = fac.createOMNamespace(namespace, tns);
		// 创建OMElement方法 元素，并指定其在nms指代的名称空间中 
		OMElement method = fac.createOMElement(methodStr, nms);
		 
		
		// 添加方法参数名和参数值 
		for (int i = 0; i < pars.length; i++) {
			// 创建方法参数OMElement元素 
			OMElement param = fac.createOMElement(pars[i], nms);
			// 设置键值对 参数值 
			param.setText(vals[i]);
			// 讲方法元素 添加到method方法元素中
			method.addChild(param);
		}
		return method;
	}

	public static Options getClientOptions(String action) {
// 创建request soap包 请求选项 
		Options options = new Options();
// 设置options的soapAction 
		options.setAction(action);
// 设置request soap包的端点引用(接口地址) 
		options.setTo(targetEpr);
// 如果报错提示Content-Length，请求内容长度 
		options.setProperty(HTTPConstants.CHUNKED, "false");// 把chunk关掉后，会自动加上Content-Length。
		return options;
	}

	public static OMElement getWeather(String action, String methodStr, String namespace, String tns, String[] pars,
			String[] vals) {
		OMElement result = null;
		try {
			ServiceClient client = new ServiceClient();
			client.setOptions(getClientOptions(action));
			result = client.sendReceive(getOMMethod(methodStr, namespace, tns, pars, vals));
		} catch (AxisFault e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void main(String[] args) {
		
//		String xml=Ajax.get(url);
//		System.out.println(xml);
//		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //创建一个DocumentBuilder的对象
		
		String targetNamespace=null;
		String prefix=null;
		
        try {
            //创建DocumentBuilder对象
//            DocumentBuilder db = dbf.newDocumentBuilder();
//            
//            
//            
//            //通过DocumentBuilder对象的parser方法加载books.xml文件到当前项目下
//            Document document = db.parse(StreamUtil.string2input(xml));
//            Node node=document.getFirstChild();
//            System.out.println(node.get);
//            System.out.println(node.getBaseURI());
//            System.out.println(node.getLocalName());
        	
        	
        	SAXReader reader = new SAXReader();
//            File file = new File("books.xml");
            Document doc = reader.read(new URL(url));
//            System.out.println(doc.asXML());
            System.out.println();
            prefix=doc.getRootElement().getNamespacePrefix();
            
//            doc.selectNodes(xpathExpression)
            
            targetNamespace=doc.getRootElement().attributeValue("targetNamespace");
            System.out.println(prefix+","+targetNamespace);
            
        }catch(Exception e)
        {
        	
        }
		
		
////		
		String methodStr = "getWeatherbyCityName";
//		String methodStr = "doDemo";
//		String namespace = "http://WebXml.com.cn/";
		String action = StringUtil.joinUrl(targetNamespace,methodStr); // "http://WebXml.com.cn/getWeatherbyCityName";
		String tns = prefix;
		String[] pars = { "theCityName" };
		String[] vals = { "北京" };
		OMElement result = null;
		result = getWeather(action, methodStr, targetNamespace, tns, pars, vals);
		System.out.println(result);
        
        
        

 
//		String methodStr = "doDemo";
//		String action = StringUtil.joinUrl(targetNamespace,methodStr); // "http://WebXml.com.cn/getWeatherbyCityName";
//		String tns = prefix;
//		String[] pars = { "arg0","arg1" };
//		String[] vals = { "北京","" };
//		OMElement result = null;
//		result = getWeather(action, methodStr, targetNamespace, tns, pars, vals);
//		System.out.println(result);
//        
        
	}
}
